package com.zzptc.zzsgz.bdsjws.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.BBXActivity;
import com.zzptc.zzsgz.bdsjws.activities.FamilyProtectActivity;
import com.zzptc.zzsgz.bdsjws.activities.MainActivity;
import com.zzptc.zzsgz.bdsjws.activities.OneKeySOSActivity;
import com.zzptc.zzsgz.bdsjws.vo.BBX;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zzsgz on 2017/5/24.
 */

public class BBXAdapter extends  RecyclerView.Adapter<BBXAdapter.MyViewHolder>{
    List<BBX> list;
    public BBXAdapter(List<BBX> list){
        this.list=list;

    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.bbx_item,parent,false);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
         final BBX bbx=  list.get(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(bbx.getName().equals("家人守护")) {
                    Intent intent = new Intent(MyApplication.getContext(), FamilyProtectActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApplication.getContext().startActivity(intent);
                }else if(bbx.getName().equals("回首页")){
                    Intent intent = new Intent(MyApplication.getContext(), MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApplication.getContext().startActivity(intent);
                }
            }
        });

        //先解决：将从数据库中取得的pic图片的String -------->Drawable  准备：放图片到res
        Context context=MyApplication.getContext();
        int pic_int= context.getResources().getIdentifier(bbx.getIcon(),"mipmap",context.getPackageName());
        try {
            //Drawable drawable = context.getResources().getDrawable(pic_int);
            Drawable drawable = ContextCompat.getDrawable(context,pic_int);
            holder.iv_bbxpic.setImageDrawable(drawable);
        }catch (Exception e){
            e.printStackTrace();
        }

        holder.tv_bbxname.setText(bbx.getName());


    }

    @Override
    public int getItemCount() {
        return list.size();
    }
  //每个项
    public class MyViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.iv_bbxpic)
        ImageView iv_bbxpic;
      @BindView(R.id.tv_bbxname)
      TextView tv_bbxname;
        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
